package Stream流练习;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class 练习二 {
    public static void main(String[] args) {
        //创建集合
        ArrayList<String> str=new ArrayList<>();
        Collections.addAll(str,"zhangsan,23","lisi,24","wangwu,25");
        //需求:
        //    保留结果大于24岁的人,并保存到MAp中键为名字,值为年龄
        Map<String, String> stringMap = str.stream().filter(s -> Integer.parseInt(s.split(",")[1]) >= 24)
                .collect(Collectors.toMap(new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        return s.split(",")[0];
                    }
                }, new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        return s.split(",")[1];
                    }
                }));

        //Lambda表达式
        str.stream().filter(s -> Integer.parseInt(s.split(",")[1]) >= 24)
                .collect(Collectors.toMap(s -> s.split(",")[0],v->v.split(",")[1]));
        //打印结果
        System.out.println(stringMap);
    }
}
